﻿#include "mainwidget.h"
#include "ui_mainwidget.h"

#include <QGuiApplication>
#include <QScreen>
#include <QImage>
#include <QPainter>
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent>
#include <iostream>

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    this->startTimer(100);
    this->ui->ScreenWidget->installEventFilter(this);
    this->ui->headerWidget->installEventFilter(this);
    this->m_bDrag = false;
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::paintEvent(QPaintEvent *event)
{

}

bool MainWidget::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == this->ui->ScreenWidget){
       if(event->type() == QEvent::Paint){
          QScreen *scr = QGuiApplication::primaryScreen();
          QPixmap pix= scr->grabWindow(QApplication::desktop()->winId());
          pix = pix.scaled(640,360, Qt::KeepAspectRatio, Qt::SmoothTransformation);
          QImage img= pix.toImage();
          QPainter p;
          p.begin(this->ui->ScreenWidget);
          p.drawImage(QPoint(0, 0), img);
          p.end();
       }
       QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
      if(event->type() == QEvent::MouseMove){
          if(m_bDrag)
          {
              //获得鼠标移动的距离
              QPoint distance = mouseEvent->globalPos() - mouseStartPoint;
              //改变窗口的位置
              ui->ScreenWidget->move(windowTopLeftPoint + distance);
          }
      }
      if(event->type() == QEvent::MouseButtonPress){
          if(mouseEvent->button() == Qt::LeftButton)
          {
              m_bDrag = true;
              //获得鼠标的初始位置
              mouseStartPoint = mouseEvent->globalPos();
              //获得窗口的初始位置
              windowTopLeftPoint = ui->ScreenWidget->frameGeometry().topLeft();
          }
      }
      if(event->type() == QEvent::MouseButtonRelease){
          if(mouseEvent->button() == Qt::LeftButton)
          {
              m_bDrag = false;
          }
      }
    }
    if(watched == this->ui->headerWidget){
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
       if(event->type() == QEvent::MouseMove){
           if(m_bDrag)
           {
               //获得鼠标移动的距离
               QPoint distance = mouseEvent->globalPos() - mouseStartPoint;
               //改变窗口的位置
               this->move(windowTopLeftPoint + distance);
           }
       }
       if(event->type() == QEvent::MouseButtonPress){
           if(mouseEvent->button() == Qt::LeftButton)
           {
               m_bDrag = true;
               //获得鼠标的初始位置
               mouseStartPoint = mouseEvent->globalPos();
               //获得窗口的初始位置
               windowTopLeftPoint = this->frameGeometry().topLeft();
           }
       }
       if(event->type() == QEvent::MouseButtonRelease){
           if(mouseEvent->button() == Qt::LeftButton)
           {
               m_bDrag = false;
           }
       }
    }
   return false;
}

void MainWidget::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event)
    update();
}

